﻿using Microsoft.AspNetCore.Mvc;
using RobosimAPI.Class;

namespace RobosimAPI.Controllers
{
	[Route("back/Api2/Client/getGroupInfo")]
	public class GetGroupInfoController : ControllerBase
	{
		[HttpPost]
		public IActionResult GetGroupInfo([FromForm] GetGroupInfoModel model)
		{
			Account account = new Account(model.user_id);
			if (!account.IsValid())
			{
				return Ok(new ErrorResponse{ status = 1, errmsg = "_INCORRECT_" });
			}
			int[] group_ids = account.GetAllJoinedEvents();
			Group group = new Group(model.group_id);
			Round[] rounds = Round.GetRoundWhereGroupId(model.group_id);
			Event event_ = new Event(group.event_id);
			GetGroupInfoResponse response = new GetGroupInfoResponse();
			if (account.face_url==null)
			{
				return Ok(new ErrorResponse{ status = 1, errmsg = "_MISSING_FACE_AUTHENTICATION_IMAGE_" });
			}
			response.status = 1;
			response.data = new Data();
			response.data.event_id = group.event_id;
			response.data.event_url = event_.event_url;
			response.data.user_id = account.user_id.ToString();
			response.data.real_name = account.real_name;
			response.data.face_url = account.face_url;
			response.data.cover = group.cover;
			response.data.need_face_auth = group.need_face_auth;
			response.data.need_face_compare = group.need_face_compare;
			response.data.can_remote = group.can_remote;
			response.data.can_build = group.can_build;
			response.data.face_timing = group.face_timing;
			response.data.applied = 0;
			response.data.is_teacher = 0;
			response.data.roundList = new List<RoundListItem>();
			response.now = Tools.GetTimestamp();
			if (Array.IndexOf(group_ids, group.id) >= 0)
			{
				response.data.applied = 1;
			}
			foreach ( Round round in rounds)
			{
				RoundListItem item = new RoundListItem();
				item.round_id = round.Id;
				item.type = round.Type;
				item.round_index = round.RoundIndex;
				item.start_time = round.StartTime;
				item.end_time = round.EndTime;
				item.preload_map_mins = round.PreloadMapMins;
				item.process = round.Process;
				response.data.roundList.Add(item);
			}
			return Ok(response);
		}
		public class GetGroupInfoModel
		{
			public int user_id { get; set; }
			public int jmd_id { get; set; }
			public int group_id { get; set; }
		}
		public class RoundListItem
		{
			public int round_id { get; set; }
			public int type { get; set; }
			public int round_index { get; set; }
			public long start_time { get; set; }
			public long end_time { get; set; }
			public int preload_map_mins { get; set; }
			public int process { get; set; }
		}
		public class Data
		{
			public string cover { get; set; }
			public string face_url { get; set; }
			public string real_name { get; set; }
			public int event_id { get; set; }
			public int need_face_auth { get; set; }
			public int need_face_compare { get; set; }
			public int can_remote { get; set; }
			public int can_build { get; set; }
			public int face_timing { get; set; }
			public string event_url { get; set; }
			public int applied { get; set; }
			public string user_id { get; set; }
			public int is_teacher { get; set; }
			public List<RoundListItem> roundList { get; set; }
		}
		public class GetGroupInfoResponse
		{
			public int status { get; set; }
			public Data data { get; set; }
			public long now { get; set; }
		}

	}
}
